﻿#include	"Engine.Guid.hpp"
#include	"Engine.String.hpp"

#if defined(_WIN32)
#	include		<objbase.h>
#else
#	include		<string.h>
#	include		<uuid/uuid.h>
#endif

namespace Engine {

	Guid::Guid() {
		::memset(_pData, 0, 16);
	}

	Guid::Guid(uint8_t * pData) {
		if (pData) ::memcpy(_pData, pData, 16);
	}

	Guid::~Guid() {}

#if defined(_WIN32)
	Guid Guid::Generate() {
		GUID	iGuid;
		Guid	iRet;

		::CoInitialize(NULL);
		::CoCreateGuid(&iGuid);
		::CoUninitialize();
		::memcpy(iRet._pData, &iGuid, 16);

		return std::move(iRet);
	}
#else
	Guid Guid::Generate() {
		uuid_t	iUUID;
		Guid	iRet;
		uuid_generate(iUUID);
		::memcpy(iRet._pData, iUUID, 16);
		return std::move(iRet);
	}
#endif

	std::string Guid::ToString() {
		uint32_t pData[4];
		::memcpy(pData, _pData, 16);
		return Engine::String::Format("%08X%08X%08X%08X", pData[0], pData[1], pData[2], pData[3]);
	}

}
